package com.easidea.tim.flex.remote.finance
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.finance.FinanceItem;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;

	public class FinanceItemDao extends BaseDao
	{
		private var _FinanceItemService:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:FinanceItemDao;
		
		public static function getInstance():FinanceItemDao{
			if( !_instance ){
				_instance = new FinanceItemDao();
				
			}
			return _instance;
		}
		public function FinanceItemDao() {
				_FinanceItemService.channelSet = this.buildChannelSet();
				_FinanceItemService.destination="financeItemService";	
				_FinanceItemService.concurrency = "last";
				_FinanceItemService.makeObjectsBindable = true;	
				_FinanceItemService.showBusyCursor = true;
		}
		
		public function getItemList():void{
			_FinanceItemService.addEventListener(ResultEvent.RESULT, getItemListResult);
			_FinanceItemService.addEventListener(FaultEvent.FAULT,_Fault);
			_FinanceItemService.getAllItemList();
		}
		
		private function getItemListResult(e:ResultEvent):void {
			_FinanceItemService.removeEventListener(ResultEvent.RESULT, getItemListResult);
			Env.dispatchEvent(new CustomerEvent("getItemListSuccess",e.result));
		}
		
		public function getItemListByCALRel(isNeed:Boolean):void{
			_FinanceItemService.addEventListener(ResultEvent.RESULT, getItemListByCALRelResult);
			_FinanceItemService.addEventListener(FaultEvent.FAULT,_Fault);
			_FinanceItemService.getItemListByCALRel(isNeed);
		}
		
		private function getItemListByCALRelResult(e:ResultEvent):void{
			_FinanceItemService.removeEventListener(ResultEvent.RESULT,getItemListByCALRelResult);
			Env.dispatchEvent(new CustomerEvent("getItemListByCALRelSuccess",e.result));
		}
		
		public function saveFinanceItem(fItem:FinanceItem):void{
			_FinanceItemService.addEventListener(ResultEvent.RESULT, saveFinanceItemResult);
			_FinanceItemService.addEventListener(FaultEvent.FAULT,_Fault);
			_FinanceItemService.saveFi(fItem);
		}
		
		private function saveFinanceItemResult(e:ResultEvent):void{
			_FinanceItemService.removeEventListener(ResultEvent.RESULT,saveFinanceItemResult);
			Env.dispatchEvent(new CustomerEvent("saveFinanceItemSuccess",e.result));
		}
		
		public function alterFI(fi:FinanceItem):void{
			_FinanceItemService.addEventListener(ResultEvent.RESULT, alterFIResult);
			_FinanceItemService.addEventListener(FaultEvent.FAULT,_Fault);
			_FinanceItemService.alterFi(fi);
		}
		
		private function alterFIResult(e:ResultEvent):void{
			_FinanceItemService.removeEventListener(ResultEvent.RESULT,alterFIResult);
			Env.dispatchEvent(new CustomerEvent("alterFISuccess",e.result));
		}
		
		public function removeFI(id:int):void{
			_FinanceItemService.addEventListener(ResultEvent.RESULT, removeFIResult);
			_FinanceItemService.addEventListener(FaultEvent.FAULT,_Fault);
			_FinanceItemService.removeFI(id);
		}
		
		private function removeFIResult(e:ResultEvent):void{
			_FinanceItemService.removeEventListener(ResultEvent.RESULT,removeFIResult);
			Env.dispatchEvent(new CustomerEvent("removeFISuccess",e.result));
		}
	}
}